 
var Time_Start; 
var Time_End; 
var is_timer_on; 
var t; 
var Now; 
 
function startTimer(){ 
	//window.localStorage.clear();   
	var d = new Date(); 
	if (!localStorage.lastcutofftime || (localStorage.lastcutofftime!= d.getDate())) 
	{ 
		//everyday quota 
		console.log("set new time!!!"); 
		localStorage.lastcutofftime = d.getDate(); 
		localStorage.remainingTime= getOptionsTime();  
	} 
	var remainingTime = localStorage.remainingTime; 
	if (remainingTime < 0){ 
			checkTimeOut(); 
			console.log("out !remaining Time" + remainingTime); 
	} 
	console.log("remaining Time" + remainingTime); 
	//resume the timer 
	updateTimeStart(); 
	is_timer_on = true; 
	t=setTimeout("checkTimeOut();",remainingTime); 
} 
 
function updateTimeStart(){ 
	Now = new Date();   
	Time_Start = Now.getTime();  
} 
 
function elapsedmilliseconds()// Calculates elapsed time 
{ 
	var n = new Date();           // Grab new copy of date 
	var s = n.getTime();          // Grab current millisecond # 
	var diff = s - Time_Start;         // Calculate the difference. 
	return diff;                  // Return the difference. 
} 
 
function stopTimer(){ 
	//dummy timer stop function 
	if (is_timer_on == true) 
		clearTimeout(t); 
	else 
		return; 
	is_timer_on = false; 
	localStorage.remainingTime= localStorage.remainingTime- elapsedmilliseconds(); 
} 
 
function updateTabData() {
  var e_url = localStorage.defaultURL;
  if (!e_url)
    e_url = "http://g.cn";
  var retVal = new Object();
  retVal.url= e_url;
  retVal.selected =true; 
  return retVal; 
} 
 
function checkTimeOut(){ 
	stopTimer(); 
	chrome.windows.getCurrent(function(window){ 
		chrome.tabs.getSelected(window.id,function(tab){
                                                        if (tab.url.indexOf("renren.com") <= 0)
								return; 
							console.log("checkout"); 
							try { 
								chrome.tabs.update(tab.id, updateTabData()); 
								} catch (e) { 
							          alert(e); 
							}; 
		}); 
	}); 
} 
 
function getOptionsTime(){ 
	if (!localStorage.browsingTime){ 
		localStorage.browsingTime = "20"; 
	} 
	var favorite = localStorage.browsingTime; 
	var TimeInMininutes=0; 
	console.log(favorite); 
	switch (favorite) 
	{ 
	case "1": 
		TimeInMininutes = 1; 
		break; 
	case "10": 
		TimeInMininutes = 10; 
		break; 
	case "20": 
		TimeInMininutes = 20; 
		break; 
	case "30": 
		TimeInMininutes = 30; 
		break; 
	case "40": 
		TimeInMininutes = 40; 
		break; 
	default: 
		TimeInMininutes = 24*60; 
	} 
	 
	return TimeInMininutes * 60 * 1000; 
}                   
 


//special care for renren.com
function checkByTabid(tabId){ 
	 chrome.tabs.get(tabId, function(tab) { 
     //window.alert(tab.url.indexOf("renren.com")); 
	 if (tab.url.indexOf("renren.com") >0 ) 
			startTimer(); 
	 else 
			stopTimer(); 
	 }); 
} 



//create one time welcome page
function showWelcomePage(){
    if (!localStorage.postInstallation){
	console.log("show welcome page here");
	var newTab = new Object();
	newTab.url = "welcome.html";;
	chrome.tabs.create(newTab, function(tab){});
	//show the welcome page once only
	localStorage.postInstallation = 1;
	console.log("where is the new tab");
    }
}



//create new tab 
chrome.tabs.onCreated.addListener(function(tab) {
  stopTimer(); 
 if (tab.url.indexOf("renren.com") > 0) 	 
	startTimer(); 
}); 

//selection in tab  
chrome.tabs.onSelectionChanged.addListener(function(tabId, changeInfo) { 
  stopTimer(); 
  checkByTabid(tabId); 
}); 

//change in tab 
chrome.tabs.onRemoved.addListener(function(tabId) { 
  //appendToLog('tabs.onRemoved -- tab: ' + tabId); 
  //loadWindowList(); 
  stopTimer(); 
}); 

//update in tab 
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo) { 
  stopTimer(); 
  checkByTabid(tabId); 
}); 

//update in window change
chrome.windows.onFocusChanged.addListener(function(windowId) {
		stopTimer();
                chrome.tabs.getSelected(windowId,function(tab){
                                                        if (tab.url.indexOf("renren.com") > 0)
								startTimer();
		});
	});

